const api = require('../../api/api.js');
const util = require('../../utils/util.js');
const app = getApp();

Page({
  data: {
    id: '',
    name: '',
    image: '',
    imgUrl: app.globalData.imgUrl
  },
  onLoad(o) {
    let category = o.category; //crop、livestock、fungus
    let id = o.id.substring(0, 15);
    let type = o.type;
    this.func(id, type, category);
  },
  func(id, type, category) {
    let that = this;
    switch (type) {
      case 'plant':
        if (category === 'crop') {
          that.queryPlanting(id);
        } else {
          that.queryPlantFungus(id);
        }
        break;
      case 'fertilizer':
        that.queryFertilizer(id);
        break;
      case 'irrigate':
        that.queryIrrigate(id);
        break;
      case 'insectAttack':
        that.queryInsectAttack(id);
        break;
      case 'addLivestock':
        that.queryAddLiveStock(id);
        break;
      case 'feed':
        that.queryFeed(id);
        break;
      case 'yimiao':
        that.queryYiMiao(id);
        break;
      case 'quarantine':
        that.queryQuarantine(id);
        break;
    }
  },
  queryPlanting(id) {
    let that = this;
    api.queryPlanting({
      data: {
        plantId: id,
        cropTypeId: -1,
        siteId: -1,
        placeId: -1,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let planting = res.data.Planting;
        let image = that.data.imgUrl + 'plantingImage/';
        let items = planting.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.plantName,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryFertilizer(id) {
    let that = this;
    api.queryFertilizer({
      data: {
        fertilizeId: -1,
        account: '',
        plantId: id
      },
      success(res) {
        console.log(res.data);
        let fertilizer = res.data.Fertilizer;
        let image = that.data.imgUrl + 'fertilizerImage/';
        let items = fertilizer.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.fertilizerName,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryIrrigate(id) {
    let that = this;
    api.queryIrrigate({
      data: {
        irrigateId: -1,
        plantId: id,
        account: '',
        type: ''
      },
      success(res) {
        console.log(res.data);
        let irrigate = res.data.Irrigate;
        let image = that.data.imgUrl + 'irrigateImage/';
        let items = irrigate.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: '灌溉',
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryInsectAttack(id) {
    let that = this;
    api.queryInsectAttack({
      data: {
        insectAttackId: -1,
        plantId: id,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let insectAttack = res.data.InsectAttack;
        let image = that.data.imgUrl + 'insectAttackImage/';
        let items = insectAttack.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.insectAttackType,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryAddLiveStock(id) {
    let that = this;
    api.queryAddLiveStock({
      data: {
        placeId: -1,
        siteId: -1,
        speciesId: -1,
        id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let addLiveStock = res.data.AddLiveStock;
        let image = that.data.imgUrl + 'addLiveStockImage/';
        let items = addLiveStock.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.name,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryFeed(id) {
    let that = this;
    api.queryFeed({
      data: {
        id: -1,
        addLivestockId: id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let feed = res.data.Feed;
        let image = that.data.imgUrl + 'feedImage/';
        let items = feed.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.type,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryYiMiao(id) {
    let that = this;
    api.queryYiMiao({
      data: {
        id: -1,
        addLivestockId: id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let yimiao = res.data.YiMiao;
        let image = that.data.imgUrl + 'yimiaoImage/';
        let items = yimiao.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.name,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryQuarantine(id) {
    let that = this;
    api.queryQuarantine({
      data: {
        id: -1,
        addLivestockId: id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let quarantine = res.data.Quarantine;
        let image = that.data.imgUrl + 'quarantineImage/';
        let items = quarantine.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: '检疫',
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  },
  queryPlantFungus(id) {
    let that = this;
    api.queryPlantFungus({
      data: {
        plantFungusId: id,
        fungusTypeId: -1,
        siteId: -1,
        placeId: -1,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let plantFungus = res.data.PlantFungus;
        let image = that.data.imgUrl + 'plantFungusImage/';
        let items = plantFungus.map(i => {
          return {
            date: i.createDate.substring(0, 11),
            name: i.plantFungusName,
            image: image + i.image
          }
        });
        that.setData({
          items
        });
      }
    });
  }
})